/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

/**
 *
 * @author ervaka
 */
@Entity
@Table(name = "ORDERS")
@NamedQueries({
    @NamedQuery(name = "TiendaliaOrder.findAll", query = "SELECT o FROM TiendaliaOrder o"),
    @NamedQuery(name = "TiendaliaOrder.findById", query = "SELECT o FROM TiendaliaOrder o WHERE o.id = :id"),
    @NamedQuery(name = "TiendaliaOrder.findByUser", query = "SELECT o FROM TiendaliaOrder o WHERE o.customer = :customer"),
    @NamedQuery(name = "TiendaliaOrder.findByTimestamp", query = "SELECT o FROM TiendaliaOrder o WHERE o.orderDate = :timestamp")})
public class TiendaliaOrder implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @GeneratedValue
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "orderDate")
    private Date orderDate;
    @NotNull
    @OneToMany(mappedBy = "order")
    private List<PurchasedProduct> orderProducts;
    @NotNull
    @ManyToOne
    @JoinColumn(name = "customer")
    private User customer;

    public TiendaliaOrder() {
        this.orderDate = new Timestamp(Calendar.getInstance().getTimeInMillis());
        this.orderProducts = new ArrayList<PurchasedProduct>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public List<PurchasedProduct> getOrderProducts() {
        return orderProducts;
    }

    public void setOrderProducts(List<PurchasedProduct> orderProducts) {
        this.orderProducts = orderProducts;
    }

    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    public Double getTotal() {
        Double total = 0d;
        for (PurchasedProduct purchasedProduct : orderProducts) {
            total += purchasedProduct.getPurchasePrice() * purchasedProduct.getQuantity();
        }
        return total;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof TiendaliaOrder)) {
            return false;
        }
        TiendaliaOrder other = (TiendaliaOrder) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.ConfirmedOrder[ id=" + id + " ]";
    }
}
